<template>
    <div>
        <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
        <!-- 添加角色 -->
        <el-row>
            <el-col>
                <el-button type="primary">添加角色</el-button>
            </el-col>
        </el-row>
     <el-table :data="roleList" border stripe>
         <el-table-column type="expand">
           <template slot-scope="scope">
                <el-row   :class="['bdbottom',i===0 ? 'bdtop':'','vceter']" v-for="(item,i) in scope.row.children"  :key="item.id">
                 <el-col :span="5">
                      <el-tag closable @close="RemoveRightById(scope.row,item.id)">
                        {{item.authName}}
                        </el-tag>
                    <i class="el-icon-caret-right"></i>

                 </el-col>
                 <el-col :span="19">
                     <el-row  :class="[i1==0?'':'bdtop', 'vceter']"   v-for="(item1,i1) in item.children" :key="item1.id">
                         <el-col :span="6">
                           <el-tag type="success" closable @close="RemoveRightById(scope.row,item1.id)">
                            {{item1.authName}}
                            </el-tag>
                             <i class="el-icon-caret-right"></i>
                         </el-col>
                         <el-col :span="18">
                             <el-tag v-for="item2 in item1.children" :key="item2.id" type="warning" closable @close="RemoveRightById(scope.row,item2.id)">{{item1.authName}}</el-tag>
                         </el-col>

                         
                     </el-row>
                 </el-col>
             </el-row>
           </template>

         </el-table-column>
         <el-table-column type="index"></el-table-column>
         <el-table-column label="角色名称" prop="roleName"></el-table-column>
         <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
         <el-table-column label="操作">
             <template slot-scope="scope">
                   <el-button type="primary" icon="el-icon-edit"  size="mini">编辑</el-button>
                       <el-button type="warning" icon="el-icon-delete"  size="mini">删除</el-button>
                        <el-tooltip content="角色分配" placement="top-start" effect="dark" :enterable="false">
                        <el-button type="danger" icon="el-icon-setting"  size="mini" @click="showSetRightDialog(scope.row)"> 分配权限</el-button>
                    </el-tooltip>
              
             </template>
         </el-table-column>

          
     </el-table>
        
        
   
    </el-card>

    <!-- 分配权限对话框 -->
    <el-dialog
        title="分配权限"
        :visible.sync="setRightDialogVisible"
        width="50%"
        @close="setRightDialogClosed"
        >
  <el-tree :data="rightsList" 
  :props="treeProps" 
  show-checkbox node-key="id" 
  default-expand-all 
  :default-checked-keys="defKeys"
  ref="treeRef"></el-tree>

  <span slot="footer" class="dialog-footer">
    <el-button @click="setRightDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="allotRights">确 定</el-button>
  </span>
</el-dialog>
    </div>
</template>
<script>
export default {
    data(){
        return {
            roleList:[],
            setRightDialogVisible:false,
            rightsList:[],
            treeProps:{
                label:'authName',
                children:'children'
            },
            defKeys:[],
            roleId:''
        }
    },
    created() {
        this.getRoleList()
    },
    methods:{
        async getRoleList(){
            const {data:res} = await this.$http.get('roles');
   
            
            if(res.meta.status !==200){
                return this.$message.error('获取用户列表失败')
            }

            this.roleList = res.data;
            // console.log(this.roleList);
            
            
        },
        RemoveRightById(role,id){
            this.$confirm('您确定要删除此项操作吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
              this.$http.delete(`roles/${role.id}/rights/${id}`)
              .then((res)=>{
                     this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    role.children = res.data.data

                    

              })
       
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
     async showSetRightDialog(role){
         this.roleId = role.id
            const {data:res} = await this.$http.get('rights/tree');

            if(res.meta.status !==200){
                return this.$message.error('获取用户权限列失败')
            }
            this.rightsList = res.data
            console.log(this.rightsList);
            
             this.getLeafKeys(role,this.defKeys)

            this.setRightDialogVisible=true
        
      },
    getLeafKeys(node,arr){
        if(!node.children){
            return arr.push(node.id)
        }
        node.children.forEach(item => {
            this.getLeafKeys(item,arr)
        });
    },
    setRightDialogClosed(){
        this.defKeys = []
    },
    async allotRights(){
        const keys=[
            ...this.$refs.treeRef.getCheckedKeys(),
            ...this.$refs.treeRef.getHalfCheckedKeys()
        ]

        const idStr = keys.join(',');
     const {data:res} = await this.$http.post(`roles/${this.roleId}/rights`,{rids:idStr});
     if(res.meta.status!==200){
         this.$message.error('分配权限失败')
     }
      this.$message.success('分配权限成功')
      this.setRightDialogVisible= false
      this.getRoleList()
    }

        }

 
    
}
</script>

<style lang="less" scope>
    .el-tag{
        margin-bottom: 7px;
        margin-top: 5px;
    }
    .bdbottom{
        border-bottom: 1px solid #eee
    }
    .bdtop{
        border-top: 1px solid #eee
    }
    .vceter{
          display: flex;
         align-items: center
    }
</style>